package org.fireflow2.designer.eclipse.parts.tree;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editparts.AbstractTreeEditPart;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.fireflow.model.ModelElement;
import org.fireflow2.designer.eclipse.Activator;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.ModelWrapper;
import org.fireflow2.designer.eclipse.policies.NodeEditPolicy;
import org.fireflow2.designer.eclipse.policies.activity.ActivityContainerEditPolicy;

public class ActivityTreePart extends AbstractTreeEditPart implements
		PropertyChangeListener {
	private Image img = null;

	public ActivityTreePart(){
		ImageDescriptor imageDescriptor = ImageDescriptor.createFromFile(Activator.class, "resources/activity.png");
		img = imageDescriptor.createImage();
	}			
	protected void refreshVisuals() {
		ModelWrapper wrapper = (ModelWrapper) this.getModel();
		ModelElement elem = wrapper.getWorkflowModelElement();
		this.setWidgetText((elem.getDisplayName() == null || elem
				.getDisplayName().equals("")) ? elem.getName() : elem
				.getDisplayName());
		this.setWidgetImage(img);
	}

	@Override
	protected void createEditPolicies() {
		// TODO Auto-generated method stub
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new NodeEditPolicy());
		installEditPolicy(EditPolicy.CONTAINER_ROLE,
				new ActivityContainerEditPolicy());
	}

//	@Override
//	protected List getModelChildren() {
//		List children = ((ActivityWrapper) this.getModel()).getChildren();
//		return children;
//	}

	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub
//		if (evt.getPropertyName().equals(AbsWrapper.TASK)||
//				evt.getPropertyName().equals(AbsWrapper.TASKREF))
//			this.refreshChildren();
//		if (evt.getPropertyName().equals(TaskWrapper.NAME)) {
//			refreshVisuals();
//		}
	}
	
	public void activate() {
		if (isActive()) {
			return;
		}
		super.activate();
		((AbsWrapper) getModel()).addPropertyChangeListener(this);
	}

	public void deactivate() {
		if (!isActive()) {
			return;
		}
		super.deactivate();
		((AbsWrapper) getModel()).removePropertyChangeListener(this);
	} 
}
